//
//  UIViewController+ShakeAndCutter.m
//  ZLMediaPlayer
//
//  Created by zhangliang on 2017/4/24.
//  Copyright © 2017年 zhangliang. All rights reserved.
//

#import "UIViewController+ShakeAndCutter.h"
#import <AudioToolbox/AudioToolbox.h>
#import <QuartzCore/QuartzCore.h>
#import "ShakeItOffController.h"

@implementation UIViewController (ShakeAndCutter)

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    
    //检测到摇动开始
    
    if (motion == UIEventSubtypeMotionShake)
        
    {
        
        // your code
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振动效果
        NSLog(@"begin animations");
        
    }
    
}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    
    //摇动取消
    NSLog(@"摇动取消");
    
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{
    
    //摇动结束
    
    if (event.subtype == UIEventSubtypeMotionShake) {
        
        // your code
        NSLog(@"摇动结束");
        [self cutterViewToDocument];
    }
    
}

- (void)cutterViewToDocument
{
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // 保存进相册
    UIImageWriteToSavedPhotosAlbum(screenShot,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

// 完成保存后回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf {
    
    //    [self.userInfo setImage:editImage];
    //    [self.tableView reloadData];
}

- (void)writeToFileWithImageData:(NSData *)data {
    // 写入文件方法一
    NSString *path1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"feedback.png"];
    NSLog(@"%@", path1);
    [data writeToFile:path1 atomically:YES];
    
    
    // 写入文件方法二：
    //NSError *error = nil;
    //NSString *path2 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"feedback.png"];
    //[data writeToFile:path2 options:NSAtomicWrite error:&error];
}


@end
